package com.DHH.Controller.Base;

import com.DHH.Mapper.Base.BaseRoleMapper;
import com.DHH.Dto.Message;
import com.DHH.Dto.PageInfo;
import com.DHH.Dto.QueryInfo;
import com.DHH.Entity.Base.BaseRole;
import com.DHH.Service.Base.IBaseRoleService;
import com.DHH.Util.ReflectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

import javax.servlet.http.HttpSession;


/**
 * Created by Administrator on 2017/4/15.
 */
@Controller
@RequestMapping("Admin/Base/BaseRole")
public class BaseRoleController {
    private Logger logger= LoggerFactory.getLogger(BaseRoleController.class);
    @Autowired
    private BaseRoleMapper baseRoleMapper;
    @Autowired
    private IBaseRoleService baseRoleService;

    @RequestMapping("/listPage")
    public String listPage(HttpSession session,ModelMap modelMap){
        return "admin/baseRole/listBaseRolePage.jsp";
    }
    @RequestMapping("/addPage")
    public String addPage(HttpSession session,ModelMap modelMap){
            return "admin/baseRole/addBaseRolePage.jsp";
    }
    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session,@RequestParam Integer id,ModelMap modelMap)
    {
        return "admin/baseRole/update{className}Page.jsp";
    }
    @RequestMapping("/detailsPage")
    public String detailPage(HttpSession session,@RequestParam Integer id,ModelMap modelMap)
    {
            return "admin/baseRole/detailsBaseRolePage.jsp";
    }

        @RequestMapping("/add")
        @ResponseBody
        public Message add(@Valid BaseRole baseRole,BindingResult result)
        {      Message msg = new Message();
                   if (result.hasErrors()) {
                          List<FieldError> err = result.getFieldErrors();
                           for (FieldError fe : err) {
                           msg.addError(fe.getField().toString(), fe.getDefaultMessage());
                            }
                     return msg;
                  }

         try{
                baseRole=baseRoleService.insertBaseRole(baseRole);
                msg.setMsg("添加成功!");
                msg.setData(baseRole);
                msg.setState(true);
                return msg;
                }catch (Exception e)
                 {   msg.setMsg("操作失败!");
                    return msg;
                 }
        }
@RequestMapping("/saveOrUpdate")
@ResponseBody
public Message saveOrUpdate(@Valid BaseRole baseRole,BindingResult result)
        {      Message msg = new Message();
        if (result.hasErrors()) {
        List<FieldError> err = result.getFieldErrors();
        for (FieldError fe : err) {
        msg.addError(fe.getField().toString(), fe.getDefaultMessage());
        }
        return msg;
        }

        try{
        baseRole=baseRoleService.saveOrUpdateBaseRole(baseRole);
        msg.setMsg("添加成功!");
        msg.setData(baseRole);
        msg.setState(true);
        return msg;
        }catch (Exception e)
        {   msg.setMsg("操作失败!");
        return msg;
        }
        }
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestParam Integer id)
    {
        Message msg=new Message();
        try{
        	BaseRole baseRole=new BaseRole();
        	baseRole.setId(id);
            baseRole=baseRoleService.deleteBaseRole(baseRole);
            msg.setMsg("操作成功");
            msg.setState(true);
            msg.setData(baseRole);
            return msg;
        }catch (Exception e)
        {   msg.setMsg("操作失败");
            return msg;
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public Message update(BaseRole baseRole)
    {
        Message msg=new Message();
        try{
            msg.setData(baseRoleService.updateBaseRole(baseRole));
            msg.setMsg("操作成功！");
            msg.setState(true);
            return msg;
        }catch (Exception e)
        {   msg.setMsg("操作失败，请稍后重试");
            return msg;
        }
    }
    @ResponseBody
    @RequestMapping("/query")
    public PageInfo<BaseRole> query(QueryInfo queryInfo) {
        System.err.println("--------------查询参数---------------");
        System.err.println(queryInfo);
        System.err.println("--------------查询参数---------------");
        /*为查询设置统一分页条件和排序条件*/
        PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
        PageHelper.orderBy(queryInfo.getSort() + " " + queryInfo.getOrder());
        if(queryInfo.getType()==null)
        {
            queryInfo.setType("search");
        }
        if(queryInfo.getCurrentPage()==null)
        {
            queryInfo.setCurrentPage(1);
        }
        if(queryInfo.getPageSize()==null)
        {
            queryInfo.setPageSize(10);
        }
        try{
            if(queryInfo.getType().equals("screen"))
             {
                   return this.screen(queryInfo);
             }
             if(queryInfo.getType().equals("filter"))
            {

                   return this.searchFilter(queryInfo);
            }
             return this.search(queryInfo);
        }catch (Exception e)
        {
            PageInfo<BaseRole> p=new PageInfo<BaseRole>();
            p.setError(e.getMessage());
            p.setMsg("查询失败，请稍后重试！");
            return p;
        }

    }



    /**
     * 查询
     * @param queryInfo
     * @return
     */
    private PageInfo<BaseRole> search(QueryInfo queryInfo)
    {   System.out.println("---------\n"+queryInfo);

        BaseRole baseRole=new BaseRole();
            /*在这里设置相关的查询参数*/
        PageInfo<BaseRole> pageInfo =new PageInfo<BaseRole>();
        Page<BaseRole> page = (Page<BaseRole>) baseRoleMapper.searchByBaseRole(baseRole);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
       return pageInfo;
    }

    /**
     * 筛选
     * @param queryInfo
     * @return
     */
    private  PageInfo<BaseRole> screen(QueryInfo queryInfo) {
    	PageInfo<BaseRole> pageInfo=new PageInfo< BaseRole>();
    	 BaseRole baseRole=new  BaseRole();
        for (String key : queryInfo.getMap().keySet()) {
               ReflectionUtils.setFieldValue(baseRole,key,queryInfo.getMap().get(key));
        }
        Page<BaseRole> page = (Page<BaseRole>) baseRoleMapper.screenByBaseRole(baseRole);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 过滤
     * @param queryInfo
     * @return
     */
    private PageInfo<BaseRole> searchFilter(QueryInfo queryInfo)
    {    BaseRole baseRole=new  BaseRole();
    	PageInfo<BaseRole> pageInfo=new PageInfo< BaseRole>();
        if(queryInfo.getFilterType()==null||queryInfo.getFilterType().length()==0) {
            queryInfo.setFilterType("number");
        }
        if(queryInfo.getFilterType().equals("number"))
        {
            //类强制转换并设置值进行操作
        }
        Page<BaseRole> page = (Page<BaseRole>)baseRoleMapper.filterByBaseRole(baseRole);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }
}

